package com.masterclient.app.businessmodule.homemodule.view.adapter.capsules;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.eoner.commonbean.banner.BannerNewItemBean;
import com.masterclient.app.R;
import com.masterclient.app.businessmodule.homemodule.view.holder.HomeViewHolder;
import com.masterclient.app.businessmodule.homemodule.old.EventAdDataCallback;
import com.masterclient.app.utils.CommonUtil;
import com.eoner.baselibrary.utils.UtilCollection;

import java.util.List;


public class HomePageXTemporaryAdapter extends DelegateAdapter.Adapter<HomeViewHolder> {

    private Context mContext;
    private LayoutHelper mLayoutHelper;
    private VirtualLayoutManager.LayoutParams mLayoutParams;
    private int mCount = 0;
    private List<BannerNewItemBean> shTemporaries;

    private EventAdDataCallback eventAdDataCallback;

    public void setEventAdDataCallback(EventAdDataCallback eventAdDataCallback) {
        this.eventAdDataCallback = eventAdDataCallback;
    }

    public HomePageXTemporaryAdapter(Context context, LayoutHelper layoutHelper, int count) {
        this(context, layoutHelper, count, null,null);
    }

    public HomePageXTemporaryAdapter(Context context, LayoutHelper layoutHelper, int count, @NonNull VirtualLayoutManager.LayoutParams layoutParams, List<BannerNewItemBean> shTemporaries) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.mCount = count;
        this.mLayoutParams = layoutParams;
        this.shTemporaries = shTemporaries;
    }
    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }
    @Override
    protected void onBindViewHolderWithOffset(HomeViewHolder holder, int position, int offsetTotal) {
    }

    @Override
    public HomeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return null;
    }
    @Override
    public void onBindViewHolder(HomeViewHolder holder, int position) {
        if(mLayoutParams!=null) {
            holder.itemView.setLayoutParams(
                    new VirtualLayoutManager.LayoutParams(mLayoutParams));
        }

        View temporary_layout = holder.itemView.findViewById(R.id.temporary_layout);
        RecyclerView sh_temporaryRecyclerView = holder.itemView.findViewById(R.id.sh_temporaryRecyclerView);

        //临时广告位
        if(shTemporaries != null && shTemporaries.size() > 0){
            temporary_layout.setVisibility(View.VISIBLE);
            BannerNewItemBean xx = shTemporaries.get(0);
            if(xx != null){
                String color = xx.getBg_color();
                if(UtilCollection.isColor(color)){
                    temporary_layout.setBackgroundColor(Color.parseColor(color));
                }
            }
            HomePageXCapsulesRemAdapter adapter = new HomePageXCapsulesRemAdapter(mContext,shTemporaries);
            LinearLayoutManager mLayoutManager = new LinearLayoutManager(mContext);
            sh_temporaryRecyclerView.setLayoutManager(mLayoutManager);
            sh_temporaryRecyclerView.setAdapter(adapter);
            adapter.setItemClickListener((mPosition) -> {
                BannerNewItemBean shTemporary = shTemporaries.get(mPosition[0]);
                eventAdDataCallback.adData(shTemporary.getS_ad_name(),shTemporary.getS_ad_id(),shTemporary.getS_group_name(),shTemporary.getS_group_id(),
                        shTemporary.getTarget(), shTemporary.getTarget_id(),mPosition[0]+"");
                CommonUtil.goNewAnyWhere(mContext,shTemporary.getTarget_platform(),shTemporary.getTarget_message(), shTemporary.getTarget(), shTemporary.getTarget_id(),
                        shTemporary.getImage(), shTemporary.getS_ad_name(), shTemporary.getS_ad_id());
            });

        }else{
            temporary_layout.setVisibility(View.GONE);
        }

    }

    @Override
    public int getItemCount() {
        return mCount;
    }

    @Override
    public int getItemViewType(int position) {
        return 13;
    }
}
